package com.org.redis.omos;

import com.org.redis.config.JedisUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.List;

/**
 * Jedis中使用事务
 */
public class TransactionDemo {

    public static void main(String[] args) {
        Jedis jedis = JedisUtils.getJedis();
        Transaction transaction = null;
        //获取一个事务
        transaction = jedis.multi();
        //在事务里进行一系列的操作
        transaction.set("a1", "t1");
        transaction.get("a1");
        transaction.set("a2", "t2");
        //执行事务
//        transaction.discard();//取消事务
        List<Object> objects = transaction.exec();
        objects.forEach(System.out::println);

        System.out.println(jedis.get("a1"));
        System.out.println(jedis.get("a2"));

        System.out.println(jedis.info());

        //关闭资源
        JedisUtils.closePool();
    }
}
